package com.genericdevice.platform.core.dialog.mvp;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import com.genericdevice.platform.R;
import com.genericdevice.platform.core.dialog.helpers.FileListHelper;
import com.genericdevice.platform.core.mvp.AbstractView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Xilent on 25.05.2014.
 */
public class FileDialogView extends AbstractView<FileDialogPresenter> {
    private ListView folderList;
    private Button backButton;
    private Button cancelButton;

    @Override
    protected void loadElements() {
        folderList = (ListView) activity.findViewById(R.id.folderList);
        backButton = (Button) activity.findViewById(R.id.back_btn);
        cancelButton = (Button) activity.findViewById(R.id.cancel_btn);

        folderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                presenter.call(position);
            }
        });

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.onBackPressed();
            }
        });

        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                activity.finish();
            }
        });
    }

    public void loadFolderList(List<Map<String, String>> list) {
        List<String> listNames = new ArrayList<String>();

        for (Map<String, String> folderEntry : list) {
            listNames.add(folderEntry.get(FileListHelper.NAME_FIELD));
        }

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, listNames);
        folderList.setAdapter(adapter);
    }

    @Override
    public int getViewResource() {
        return R.layout.file_dialog;
    }
}
